unit uFrmCage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Grids, StdCtrls, uUtils, ExtCtrls, Buttons, gnugettext, uFrmPlayerWin;

type
  TfrmCage = class(TForm)
    Label3: TLabel;
    stg_Players: TStringGrid;
    dtp_Date: TDateTimePicker;
    Label4: TLabel;
    stg_Tournaments: TStringGrid;
    Label2: TLabel;
    stg_TableOcupation: TStringGrid;
    Label1: TLabel;
    chb_ShowFromAllTables: TCheckBox;
    com_OrderTicketsBy: TComboBox;
    Label5: TLabel;
    img_Player: TImage;
    btn_PlayerWinner: TSpeedButton;
    btn_PlayerFinalist: TSpeedButton;
    btn_PlayerOut: TSpeedButton;
    btn_PlayerOnTable: TSpeedButton;
    btn_ResetState: TSpeedButton;
    btn_DeleteTicket: TSpeedButton;
    btn_BuyIn: TSpeedButton;
    btn_ReBuy: TSpeedButton;
    Label7: TLabel;
    GroupBox1: TGroupBox;
    btn_TournamentPause: TSpeedButton;
    btn_TournamentContinue: TSpeedButton;
    btn_PreBuy: TSpeedButton;
    procedure dtp_DateChange(Sender: TObject);
    procedure btn_TicketSellClick(Sender: TObject);
    procedure btn_DeleteTicketClick(Sender: TObject);
    procedure stg_TournamentsSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure btn_PlayerOnTableClick(Sender: TObject);
    procedure btn_PlayerOutClick(Sender: TObject);
    procedure btn_PlayerWinnerClick(Sender: TObject);
    procedure btn_PlayerFinalistClick(Sender: TObject);
    procedure btn_ResetStateClick(Sender: TObject);
    procedure stg_TableOcupationSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure chb_ShowFromAllTablesClick(Sender: TObject);
    procedure com_OrderTicketsByChange(Sender: TObject);
    procedure stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    CanSelect: Boolean;
    procedure GetTicketsData(TournamentId: string; TableId: string = '');
    procedure PopulateTicketOrderCombo();
    procedure OperationAtTickets(Msg, UpdateState: string);
  public
    { Public declarations }
  end;

var
  frmCage: TfrmCage;

implementation

uses uFrmMain, uFrmSellTicket;
{$R *.dfm}

procedure TfrmCage.stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  TicketID: string;
begin
  TicketID := stg_Players.Cells[0, ARow];
  if TicketID <> '' then
  begin
    if ARow > 1 then
    begin
      img_Player.Picture := uUtils.GetImage('select IMAGE, IMAGE_TYPE from players where id=(select player_id from tickets where id=' + TicketID + ')', FrmMain.OracleSession1);
      btn_PlayerFinalist.Enabled := true;
      btn_PlayerOut.Enabled := true;
      btn_PlayerOnTable.Enabled := true;
      btn_ResetState.Enabled := true;
      btn_DeleteTicket.Enabled := true;
      btn_PlayerWinner.Enabled := true;
    end;
  end
  else
  begin
    img_Player.Picture := nil;
    btn_PlayerFinalist.Enabled := false;
    btn_PlayerOut.Enabled := false;
    btn_PlayerOnTable.Enabled := false;
    btn_ResetState.Enabled := false;
    btn_PlayerWinner.Enabled := false;
    btn_DeleteTicket.Enabled := false;
  end;
end;

procedure TfrmCage.stg_TableOcupationSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  GetTicketsData(stg_Tournaments.Cells[0, stg_Tournaments.Row], stg_TableOcupation.Cells[0, ARow]);
end;

procedure TfrmCage.stg_TournamentsSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
var
  TournamentId: string;
begin
  uUtils.GetGridData(stg_TableOcupation, '', false);
  uUtils.GetGridData(stg_Players, '', false);
  TournamentId := stg_Tournaments.Cells[0, ARow];
  if TournamentId <> '' then
  begin
    GetGridData(stg_TableOcupation,
      'select t.id, (t.seats-count(ti.id)) as seats_free,t.seats, t.name  from TOURNAMENT_TABLES tt left join tables t on tt.TABLE_ID = t.ID' +
        ' left join (select * from tickets where TOURNAMENT_ID=' + TournamentId + ' and deleted=0) ti on ti.TABLE_ID=t.ID where tt.TOURNAMENT_ID = ' + TournamentId +
        ' group by t.seats,t.name, t.id, t.seats', false);
    GetTicketsData(TournamentId, stg_TableOcupation.Cells[0, stg_TableOcupation.Row]);
    if dtp_Date.Date > now() then
    begin
      btn_BuyIn.Enabled := false;
      btn_ReBuy.Enabled := false;
      btn_PreBuy.Enabled := true;
    end
    else
    begin
      btn_BuyIn.Enabled := true;
      btn_ReBuy.Enabled := true;
      btn_PreBuy.Enabled := false;
    end;
  end
  else
  begin
    btn_ReBuy.Enabled := false;
    btn_BuyIn.Enabled := false;
    btn_PreBuy.Enabled := false;
  end;
end;

procedure TfrmCage.GetTicketsData(TournamentId: string; TableId: string = '');
var
  Query, WhereCond, OrderBy: string;
begin
  uUtils.GetGridData(stg_Players, '', true);
  if TournamentId <> '' then
  begin
    if (TableId <> '') and (not chb_ShowFromAllTables.Checked) then
    begin
      WhereCond := ' and ti.table_id=' + TableId;
    end
    else
    begin
      WhereCond := '';
    end;
    if com_OrderTicketsBy.Text <> '' then
    begin
      OrderBy := ' order by ' + com_OrderTicketsBy.Text;
    end
    else
    begin
      OrderBy := '';
    end;
    Query := ('select ti.id, pl.name ||'' ''||pl.surname as name , pl.birth_date, ta.name as tble, ti.SEAT_NUM, ti.state, ti.ticket_type from tickets ti join players pl on ti.PLAYER_ID=pl.id join tables ta on ta.id=ti.table_id where deleted=0 and tournament_id=' + TournamentId + WhereCond + OrderBy);
    GetGridData(stg_Players, Query, true);
    PopulateTicketOrderCombo();
    stg_PlayersSelectCell(self, 0, stg_Players.Row, CanSelect);
  end;
end;

procedure TfrmCage.PopulateTicketOrderCombo();
var
  c: Integer;
begin
  if com_OrderTicketsBy.Items.Count < 1 then
  begin
    com_OrderTicketsBy.Items.Clear;
    com_OrderTicketsBy.Items.Add('');
    for c := 0 to stg_Players.ColCount - 1 do
    begin
      com_OrderTicketsBy.Items.Add(stg_Players.Cells[c, 0]);
    end;
  end;
end;

procedure TfrmCage.btn_DeleteTicketClick(Sender: TObject);
begin
  if MessageDlg('Delete record?', mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    FrmMain.UpdateDataToDB('TICKETS', 'DELETED=1', 'ID=' + stg_Players.Cells[0, stg_Players.Row]);
    stg_TournamentsSelectCell(self, 0, 2, CanSelect);
  end;
end;

procedure TfrmCage.OperationAtTickets(Msg, UpdateState: string);
begin
  if MessageDlg(Msg, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    if stg_Players.Cells[0, stg_Players.Row] <> '' then
    begin
      if UpdateState = uUtils.PlayerIsWinner then
      begin
        if FrmPlayerWin.ShowModal = mrOK then
        begin
          FrmMain.UpdateDataToDB('TICKETS', 'STATE=' + '''' + UpdateState + '''', 'ID=' + stg_Players.Cells[0, stg_Players.Row]);
        end;
      end
      else
      begin
        FrmMain.UpdateDataToDB('TICKETS', 'STATE=' + '''' + UpdateState + '''', 'ID=' + stg_Players.Cells[0, stg_Players.Row]);
      end;
      stg_TournamentsSelectCell(self, 0, 2, CanSelect);
    end;
  end;
end;

procedure TfrmCage.btn_PlayerFinalistClick(Sender: TObject);
begin
  OperationAtTickets('Confirm Finalist?', uUtils.PlayerIsFinalist);
end;

procedure TfrmCage.btn_PlayerOnTableClick(Sender: TObject);
begin
  OperationAtTickets('Confirm Player On Table?', uUtils.PlayerOnTable);
end;

procedure TfrmCage.btn_PlayerOutClick(Sender: TObject);
begin
  OperationAtTickets('Confirm Player Out?', uUtils.PlayerOutFromTable);
end;

procedure TfrmCage.btn_PlayerWinnerClick(Sender: TObject);
begin
  OperationAtTickets('Confirm Winner', uUtils.PlayerIsWinner);
end;

procedure TfrmCage.btn_ResetStateClick(Sender: TObject);
begin
  OperationAtTickets('Confirm Reset state', uUtils.PlayerWithTicket);
end;

procedure TfrmCage.chb_ShowFromAllTablesClick(Sender: TObject);
begin
  GetTicketsData(stg_Tournaments.Cells[0, stg_Tournaments.Row], stg_TableOcupation.Cells[0, stg_TableOcupation.Row]);
end;

procedure TfrmCage.com_OrderTicketsByChange(Sender: TObject);
begin
  GetTicketsData(stg_Tournaments.Cells[0, stg_Tournaments.Row], stg_TableOcupation.Cells[0, stg_TableOcupation.Row]);
end;

procedure TfrmCage.dtp_DateChange(Sender: TObject);
var
  WhereCond, Query: string;
begin
  WhereCond := 'where to_date(' + '''' + FormatDateTime('dd.mm.yyyy', dtp_Date.Date) + '''' + ',' + '''dd.mm.yyyy''' + ') between TRUNC(event_start) and TRUNC(event_end)';
  Query := 'select tr.id, tr.name, buy_in, event_start, event_end, buys_count,money_prize, sum_fee, sum_rake from tournaments tr' +
    ' left join (select count(1) as buys_count,sum(buy_in) as money_prize, sum(fee) as sum_fee, sum(rake) as sum_rake, tournament_id from tickets ti where ti.DELETED=0 group by tournament_id) ti on ti.TOURNAMENT_ID = tr.id ' + WhereCond;
  uUtils.GetGridData(stg_Tournaments, Query, true);
  stg_TournamentsSelectCell(stg_Tournaments, 0, 2, CanSelect);
  stg_PlayersSelectCell(stg_Players, 0, 2, CanSelect);
end;

procedure TfrmCage.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
end;

procedure TfrmCage.btn_TicketSellClick(Sender: TObject);
var
  TicketType: string;
begin
  if TButton(Sender).Name = btn_BuyIn.Name then
    TicketType := 'BUY'
  else if TButton(Sender).Name = btn_PreBuy.Name then
    TicketType := 'PRE'
  else if TButton(Sender).Name = btn_ReBuy.Name then
    TicketType := 'RE';
  if stg_Tournaments.Cells[0, stg_Tournaments.Row] <> '' then
  begin
    FrmMain.OracleQuery1.SQL.Clear;
    FrmMain.OracleQuery1.SQL.Add('SELECT BUY_IN, FEE, RAKE FROM TOURNAMENTS WHERE ID=' + stg_Tournaments.Cells[0, stg_Tournaments.Row]);
    FrmMain.OracleQuery1.Execute;
    frm_SellTicket.ShowSellTicketForm(stg_Tournaments.Cells[0, stg_Tournaments.Row], FrmMain.OracleQuery1.Field('BUY_IN'), FrmMain.OracleQuery1.Field('FEE'), FrmMain.OracleQuery1.Field('RAKE'),
      TicketType);
  end;
end;

end.
